/*
*	@file
*	@author  Thomas Kroes <t.kroes at tudelft.nl>
*	@version 1.0
*	
*	@section LICENSE
*	
*	Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*	
*	Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
*	Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
*	Neither the name of the TU Delft nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
*
*	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*	
*	Part of the SSAO code is based on the tutorial by Michael Belanec: http://www.belanecbn.sk/opengl_tutorialy/index.php?id=30
*	The bloom filter code is based on code snippets from chapter 8 in "iPhone 3D Programming: http://ofps.oreilly.com/titles/9780596804824/chadvanced.html"
*
*/

#pragma once

#include <vtkSmartPointer.h>
#include <vtk3DSImporter.h>
#include <vtkSTLReader.h>
#include <vtkPolyDataNormals.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderer.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkLightsPass.h>
#include <vtkOpaquePass.h>
#include <vtkRenderPassCollection.h>
#include <vtkSequencePass.h>
#include <vtkCameraPass.h>
#include <vtkCallbackCommand.h>
#include <vtkCamera.h>
#include <vtkLight.h>
#include <vtkWindowToImageFilter.h>
#include <vtkPNGWriter.h>
#include <vtkSphereSource.h>
#include <vtkJPEGReader.h>
#include <vtkTexture.h>
#include <vtkTextureMapToSphere.h>
#include <vtkPolyDataMapper.h>

#include "vtkBloomPass.h"

int NoScreenShots = 0;

void Load3dsModel(const char* FilePath, vtkRenderWindow* RenderWindow);
void LoadStlModel(const char* FilePath, vtkRenderer* Renderer);
void KeypressCallbackFunction(vtkObject* Caller, long unsigned int EventID, void* ClientData, void* CallData);
void SaveScreenShot(vtkRenderWindow* RenderWindow);

int main(int argc, char *argv[])
{
	// Create renderer
	vtkSmartPointer<vtkRenderer> Renderer = vtkSmartPointer<vtkRenderer>::New();
	Renderer->SetBackground(0.3f, 0.2f, 0.1f);
	Renderer->SetBackground2(0.1, 0.1, 0.1);
	Renderer->SetGradientBackground(true);

	// Create render window
	vtkSmartPointer<vtkRenderWindow> RenderWindow = vtkSmartPointer<vtkRenderWindow>::New();
	RenderWindow->AddRenderer(Renderer);
	RenderWindow->SetSize(512, 512);

	// Create render window interactor
	vtkSmartPointer<vtkRenderWindowInteractor> RenderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
	RenderWindowInteractor->SetRenderWindow(RenderWindow);

	// Adjust interaction style	
	vtkSmartPointer<vtkInteractorStyleTrackballCamera> TrackballCamera = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
	RenderWindowInteractor->SetInteractorStyle(TrackballCamera);
	
	// Multi-pass rendering
	vtkSmartPointer<vtkLightsPass>				LightsPass		= vtkLightsPass::New();
	vtkSmartPointer<vtkOpaquePass>				DefaultPass		= vtkOpaquePass::New();
	vtkSmartPointer<vtkRenderPassCollection>	Passes			= vtkRenderPassCollection::New();
	vtkSmartPointer<vtkSequencePass>			SequencePass	= vtkSequencePass::New();
	vtkSmartPointer<vtkCameraPass>				CameraPass		= vtkCameraPass::New();
	vtkSmartPointer<vtkBloomPass>				BloomPass		= vtkBloomPass::New();
	
	// Configure passes
	Passes->AddItem(LightsPass);
	Passes->AddItem(DefaultPass);
	SequencePass->SetPasses(Passes);
	CameraPass->SetDelegatePass(SequencePass);

	// Bloom pass configuration
	BloomPass->SetDelegatePass(CameraPass);
	BloomPass->SetFilterColor(0.9f, 0.7f, 0.4f);
	
	// Apply pass
	Renderer->SetPass(BloomPass);
	
	Renderer->ResetCamera();

	// Use perspective projection
	Renderer->GetActiveCamera()->SetParallelProjection(0);

	// Adjust lighting
	Renderer->RemoveAllLights();
	
	vtkLight* Light = Renderer->MakeLight();
	Light->SetPositional(1);
	Light->SetPosition(100.0f, 100.0f, 100.0f);
	
	// Load 3D model
	LoadStlModel("C://Workspaces//vtkrenderpasses//source//models//example.stl", Renderer);

	// Fit camera to scene
	Renderer->ResetCamera();

	// Start rendering
	RenderWindow->Render();
	RenderWindowInteractor->Start();

	return EXIT_SUCCESS;
}

void Load3dsModel(const char* FilePath, vtkRenderWindow* RenderWindow)
{
	printf("Loading: %s\n", FilePath);

	vtkSmartPointer<vtk3DSImporter> Importer = vtk3DSImporter::New();
	Importer->SetFileName(FilePath);
	Importer->SetComputeNormals(1);
	Importer->SetRenderWindow(RenderWindow);
	Importer->Read();
}

void LoadStlModel(const char* FilePath, vtkRenderer* Renderer)
{
	printf("Loading: %s\n", FilePath);

	vtkSmartPointer<vtkSTLReader> Reader = vtkSmartPointer<vtkSTLReader>::New();
	Reader->SetFileName(FilePath);
	Reader->Update();

	vtkSmartPointer<vtkPolyDataNormals> PolyDataNormals = vtkPolyDataNormals::New();
	PolyDataNormals->SetInput(Reader->GetOutput());
	PolyDataNormals->SetFeatureAngle(45.0);
	PolyDataNormals->AutoOrientNormalsOff();
	PolyDataNormals->ComputePointNormalsOn();
	PolyDataNormals->ComputeCellNormalsOn();
	PolyDataNormals->Update();

	vtkSmartPointer<vtkPolyDataMapper> Mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
	Mapper->SetInputConnection(Reader->GetOutputPort());

	vtkSmartPointer<vtkActor> Actor = vtkSmartPointer<vtkActor>::New();
	Actor->SetMapper(Mapper);
	Actor->GetProperty()->SetColor(0.5f, 0.5f, 0.5f);
	Actor->GetProperty()->SetSpecularColor(1.0f, 1.0f, 1.0f);
	Actor->GetProperty()->SetAmbientColor(0.5f, 0.5f, 0.5f);
	Actor->GetProperty()->SetAmbient(0.2f);
	Actor->GetProperty()->SetSpecular(0.8f);
	Actor->GetProperty()->SetSpecularPower(1000);

	Renderer->AddActor(Actor);
}

void SaveScreenShot(vtkRenderWindow* RenderWindow)
{
	vtkSmartPointer<vtkWindowToImageFilter> WindowToImage = vtkWindowToImageFilter::New();

	WindowToImage->SetInput(RenderWindow);
	WindowToImage->Update();

	vtkSmartPointer<vtkPNGWriter> ImageWriter = vtkPNGWriter::New();

	char FilePath[256];
	sprintf_s(FilePath, 256, "c:\\screenshot_%d.png", NoScreenShots);

	ImageWriter->SetInput(WindowToImage->GetOutput());
	ImageWriter->SetFileName(FilePath);
	ImageWriter->Update();
	ImageWriter->Write();

	NoScreenShots++;
}